using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;
using System.Data.SqlClient;
using Microsoft.SharedSource.SqlServer.MDDE.Middleware;

namespace Microsoft.SharedSource.SqlServer.MDDE.ManagementStudio
{
    internal partial class UpgradeTemplateSelectPackages : Microsoft.SharedSource.SqlServer.MDDE.ManagementStudio.UpgradeTemplateBase
    {
        internal UpgradeTemplateSelectPackages(UpgradeTemplateWizardController controller)
            : base(controller)
        {
            InitializeComponent();
        }

        private void UpgradeTemplateSelectPackages_Load(object sender, EventArgs e)
        {
            // Get the repository connection string and template name from controller.
            string repositoryConnectionString = controller.RepositoryConnectionString;
            string templateName = controller.WizardConfigurations.SelectSingleNode("//Form[@Name=\"GetTemplateLocation\"]/UserInput[@Name=\"NewTemplateName\"]").Attributes["Value"].Value;

            // Get the package list.
            XmlDocument packages =
                MDDEManager.GetPackages(repositoryConnectionString, controller.TemplateName);

            // Populate the main grid.
            foreach (XmlNode xmlPackage in packages.SelectNodes("//Package"))
                dgSelectPackages.Rows.Add(false, xmlPackage.Attributes["Name"].Value,
                    templateName + "_" + xmlPackage.Attributes["Name"].Value);
        }

        protected override void btnNext_Click(object sender, EventArgs e)
        {
            // Locate any earlier entry for this form.
            XmlNode formSelectPackages =
                controller.WizardConfigurations.SelectSingleNode("//Form[@Name=\"SelectPackages\"]");

            // Delete the earlier node if found.
            if (formSelectPackages != null)
                formSelectPackages.ParentNode.RemoveChild(formSelectPackages);

            // Create the form configuration node in the wizard configuration.
            XmlNode formNode =
                UIHelper.AppendChildElement(controller.WizardConfigurations.FirstChild, "Form",
                new string[] { "Name", "SelectPackages" });

            // Add in each selected packages.
            foreach (DataGridViewRow r0 in dgSelectPackages.Rows)
            {
                if (Convert.ToBoolean(r0.Cells["Select"].Value))
                {
                    UIHelper.AppendChildElement(formNode, "UserInput", 
                        new string[] { "SourcePackageName", r0.Cells["Source"].Value.ToString(),
                            "NewPackageName", r0.Cells["NewPackageName"].Value.ToString()});
                }
            }
            
            // Invoke the base method.
            base.btnNext_Click(sender, e);
        }

        private void chkSelect_Click(object sender, EventArgs e)
        {
            // Apply the selected change to each of the checkbox.
            foreach (DataGridViewRow r0 in this.dgSelectPackages.Rows)
                r0.Cells["Select"].Value = this.chkSelect.Checked;
        }

        private void dgSelectPackages_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // If the row check box is being deselected, deselect the main check box as well.
            if (e.ColumnIndex == 0)
            {
                if (chkSelect.Checked &&
                    !Convert.ToBoolean(
                    dgSelectPackages.Rows[e.RowIndex].Cells[0].EditedFormattedValue))
                    chkSelect.Checked = false;
            }
        }
    }
}

